var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var helperOpen = require("helper/open");

var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var noData = [{"template":1,"labelContent":"暂无动态"}];
//声明UI变量
var listView=ui("listView");
var listdata = mm("do_ListData");
listView.bindItems(listdata);//绑定item的数据

/**********加载页头begin***********/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage();
});
ui(addheader+".labelTitle").text = "我的邻里动态";
/**********加载页头end***********/
do_Page.on("back", function() {
	do_App.closePage();
})

do_Page.on("loaded", function(){
	getFirstPageData();
});

//本页面-ajax业务请求url
var httpUrl = communityUrl.url.message.managePage;
var pageNum=1;

//当页面加载完毕后执行
do_Page.on("loaded", function() {
	getFirstPageData();
});


//全部刷新数据
function getFirstPageData(){
	pageNum = 1;
	var params={page:pageNum};
	qqHttp.post(httpUrl,params,function(data){
		listView.rebound();
		listdata.removeAll();
		if(data.page.content.length){
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		listView.refreshItems();//刷新item数据
	});
}

//刷新数据
function getNextPageData() {
	pageNum++;
	var params={page:pageNum};
	qqHttp.post(httpUrl,params,function(data){
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();//刷新item数据
	});
}
//下拉列表，刷新数据
listView.on("pull", function(data){
	if (data.state == 2){
		getFirstPageData();
	}
});

//上拉列表，翻页数据
listView.on("push", function(data){
	if (data.state == 2){
		getNextPageData();
	}
});
do_Page.on("deleteMessage", function(data) {
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==data){
			listdata.removeData([i]);
			listView.refreshItems();
			break;
		}
	}
	if(alldata.length==1){
		listdata.addData(noData);
		listView.refreshItems();
	}
})
// 点击一条数据
listView.on("touch", function(data) {
	var ondata = listdata.getOne(data);
	helperOpen.open("source://view/community/message/detail/msgBorderView.ui", {
		id : ondata.id
	});
});
